/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//Simbolo  String
//Prefijo o sufijo

package bsc.war.form;

import bsc.ejb.metadata.Framework;
import bsc.ejb.metadata.SMTP;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class NewSMTP extends AbstractController{

     @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
             HttpServletResponse response) throws Exception {

                boolean edit = false;
                Long smtpId = -1L;

                if(NumberUtils.isDigits(request.getParameter("id"))){
                    Long id = Long.valueOf(request.getParameter("id"));
                    if(id >= 0){
                        edit = true;
                        smtpId = id;
                    }
                }

                HttpSession session = request.getSession();
                ModelAndView mav = new ModelAndView("renderXml");

                DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
                SMTP smtp = null;


                 Framework framework = null;
            if(session.getAttribute("frameworkId") instanceof Long){

                Long frameworkId = (Long)session.getAttribute("frameworkId");
                framework = (Framework) Factory.getBean("Framework");
                framework = delegate.findFramework(Long.valueOf(frameworkId));

            }else{
                throw new Exception("no hay frameworkId");
            }


                if(edit){
                    smtp = delegate.findSMTP(smtpId);
                }else{
                    smtp = (SMTP) Factory.getBean("SMTP");
                }




                smtp.setName(request.getParameter("name"));
                smtp.setDescription(request.getParameter("description"));
                smtp.setMail(request.getParameter("mail"));
                smtp.setPassword(request.getParameter("password"));
                smtp.setSmtpServer(request.getParameter("smtp"));
                smtp.setFramework(framework);
              

                if(edit){
                    delegate.updateSMTP(smtp);
                }else{
                  delegate.createSMTP(smtp);
                }


          mav.addObject("id",smtp.getIdSMTP());

         return mav;
    }

}
